package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.POJO.OrderSetting;
import com.itheima.dao.ordersettingMapper;
import com.itheima.service.ordersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ordersettingService.class)
@Transactional
public class ordersettingServiceImpl implements ordersettingService {
    @Autowired
    private ordersettingMapper ordersettingMapper;
    /**
     * 保存可预约时间和可预约人数到ordersetting数据库
     * @param orderSettingArrayList
     */
    @Override
    public void addOrdersetting3orderDateAndnumber(ArrayList<OrderSetting> orderSettingArrayList) {
        if (orderSettingArrayList!=null && orderSettingArrayList.size()>0){
            for (OrderSetting orderSetting : orderSettingArrayList) {
                //去数据库查 ordersetting 有没有这个预约时间
                Integer count = ordersettingMapper.findOrderdataFromordersetting(orderSetting.getOrderDate());
                if (count>0){
                    //如果有那就更新 可预约人数
                    ordersettingMapper.editNumber4OderData4Ordersetting(orderSetting);
                }else {
                    //如果没有就新增  可预约人数
                    ordersettingMapper.addOrdersetting3orderDateAndnumber(orderSetting);
                }
            }
        }else {
            throw  new RuntimeException("数据为空,无法保存,上传失败");
        }
    }

    /**
     * 得到所有的ordersetting 对象  以前端指定的格式传给前端
     * @param date
     * @return
     */
    @Override
    public List<Map> findOrdersettingBydate(String date) {// 格式为 2019-11
        /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/

        String bagin=date+"-1";//  2019/11/1
        String end=date+"-31";//   2019/11/31
     //  List<Map> mapArrayList= ordersettingMapper.findOrdersettingBydata(bagin,end);
       List<OrderSetting> orderSettingList= ordersettingMapper.findOrdersettingBydata(bagin,end);



        List<Map> mapArrayList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            mapArrayList.add(map);
        }

        return mapArrayList;
    }

    @Override
    public void setOrdersetting(OrderSetting orderSetting) {
        try {
            Date orderDate = orderSetting.getOrderDate();
            Integer count = ordersettingMapper.findOrderdataFromordersetting(orderDate);
            if (count>0){
                ordersettingMapper.editNumber4OderData4Ordersetting(orderSetting);
            }else {
                ordersettingMapper.addOrdersetting3orderDateAndnumber(orderSetting);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException("设置出错,请联系管理员");
        }
    }

}
